package com.sam.im.heiqun.uis.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialcamera.internal.PlayNewActivity;
import com.bumptech.glide.Glide;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.MyCircleItem;
import com.sam.im.heiqun.mvp.modle.PhotoInfo;
import com.sam.im.heiqun.uis.activities.CircleActivity;
import com.sam.im.heiqun.uis.activities.CirclealldetailActivity;
import com.sam.im.heiqun.uis.activities.ImagePagerActivity;
import com.sam.im.heiqun.uis.adapters.holder.ImageViewHolder;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.sam.im.heiqun.widgets.MultiImageView;
import com.yuyh.library.utils.GlideUtils;
import com.yuyh.library.utils.TimeUtil;
import com.yuyh.library.view.image.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import com.sam.im.heiqun.utils.StringUtils;

/**
 * Created by leijiawen on 2017/6/12.
 */
public class MycircleAdapter extends BaseRecycleViewAdapter {
    public final static int TYPE_HEAD = 0;//头部
    public final static int TYPE_BODY = 1 ;
    private Context context ;
    private LayoutInflater inflater ;
    private String friendname ="" ;
    private String friendurl ="";
    private String friendid ="";
    private OnbackgroudClickListenler onbackgroudClickListenler ;
    private String backgroudUrl  ;

    private boolean isShowGift = false ;

    public void setShowGift(boolean isShowGift){
        this.isShowGift = isShowGift ;
    }

    public void setBackgroudUrl(String backgroudUrl) {
        this.backgroudUrl = backgroudUrl;
        notifyDataSetChanged();
    }

    public void setOnbackgroudClickListenler(OnbackgroudClickListenler onbackgroudClickListenler) {
        this.onbackgroudClickListenler = onbackgroudClickListenler;
    }
    public void setFriendname(String friendname,String friendurl, String friendid) {
        this.friendname = friendname;
        this.friendurl = friendurl;
        this.friendid = friendid;
        notifyDataSetChanged();
    }


    public MycircleAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null ;
        if (viewType==TYPE_HEAD){
            View view = inflater.inflate(R.layout.head_circle, parent, false);
            holder =new HeaderViewHolder(view);
        }else {
            View view = inflater.inflate(R.layout.activity_mycircle_activty,parent,false);
            holder = new BodyViewHolder(view);
        }
        return holder;
    }
    public class BodyViewHolder extends  RecyclerView.ViewHolder{
        private RelativeLayout layout_content ;
        private TextView tv_date ;
        private TextView tv_mouth ;
        private TextView tv_content ;
        private TextView tv_nums  ;
        public  ImageView multiImageView ;
        public  ImageView image_startvideo ;
        public TextView tvTxt ;
        public BodyViewHolder(View itemView) {
            super(itemView);
            layout_content = (RelativeLayout) itemView.findViewById(R.id.layout_content);
            tv_date = (TextView) itemView.findViewById(R.id.tv_ddate);
            tv_mouth = (TextView) itemView.findViewById(R.id.tv_mouth);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
            tv_nums = (TextView) itemView.findViewById(R.id.tv_nums);
            multiImageView = (ImageView) itemView.findViewById(R.id.multiimg);
            image_startvideo = (ImageView) itemView.findViewById(R.id.image_startvideo);
            tvTxt = (TextView) itemView.findViewById(R.id.tv_txt);
        }
    }
    class HeaderViewHolder extends RecyclerView.ViewHolder{
        ImageView imbg ;
        ImageView image_gift ;
        CircleImageView imicon ;
        TextView tvname ;
        public HeaderViewHolder(View itemView) {
            super(itemView);
            imbg = (ImageView) itemView.findViewById(R.id.img_bg);
            image_gift = (ImageView) itemView.findViewById(R.id.image_gift);
            imicon = (CircleImageView) itemView.findViewById(R.id.imicon);
            tvname = (TextView) itemView.findViewById(R.id.tvname);
        }
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position)==TYPE_HEAD){
            setheadlayout((HeaderViewHolder)holder);
        }else {
            setbodylayout((BodyViewHolder)holder,position-1);
        }
    }

    private void setbodylayout(BodyViewHolder holder, int position) {
        List<MyCircleItem.ListBean> data = (List<MyCircleItem.ListBean>)datas;
        final MyCircleItem.ListBean listBean = data.get(position);
        final String feedId = listBean.getFeedId();
        String feedImgs = listBean.getFeedImgs();
        String createTime = listBean.getCreateTime();//时间
        long l = Long.parseLong(createTime);
        String allTimeNoSecond = TimeUtil.getYMdTime(l);

        String[] split = allTimeNoSecond.split("-");
        holder.tv_date.setText(split[2]);
        holder.tv_mouth.setText(split[1]+"月");
        //处理图片
        final List<PhotoInfo> photos = new ArrayList<>();

        int thisCircleType = 0 ;//0-文字图片；1-视频；2-URL

        if (null!= listBean && listBean.getLId().equals("-1")){

            try {
                holder.image_startvideo.setVisibility(View.GONE);
            }catch (Exception e){
                e.printStackTrace();
            }
            holder.tv_nums.setVisibility(View.GONE);
            holder.tv_content.setVisibility(View.GONE);
            holder.tvTxt.setVisibility(View.GONE);

//            holder.multiImageView.setBackground(null);
            holder.multiImageView.setVisibility(View.VISIBLE);
//            Glide.with(context).load(photos.get(0).url).centerCrop().into(holder.multiImageView);
//            holder.multiImageView.setBackgroundResource(R.drawable.ic_addpic);
            GlideUtils.loadImageCircleAdd(context, "", holder.multiImageView);

            holder.multiImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onbackgroudClickListenler.backgroudclick(v,3);
                }
            });

        }else {
            if (listBean.getFeedVideo().length() > 0 && feedImgs.length() > 0){
                thisCircleType = 1 ;
            }else {
                thisCircleType = 0 ;
            }
            try {
                holder.image_startvideo.setVisibility(View.GONE);
            }catch (Exception e){
                e.printStackTrace();
            }

            String feedText = listBean.getFeedText();
            switch (thisCircleType){
                case 0 ://文字图片
                    if (!StringUtils.isEmpty(feedImgs)) {
                        if (feedImgs.contains(",")) {
                            String[] splitimg = feedImgs.split(",");
                            for (int e = 0; e < splitimg.length; e++) {
                                PhotoInfo photoInfo = new PhotoInfo();
                                photoInfo.url = splitimg[e];
                                photos.add(photoInfo);
                            }
                        } else {
                            PhotoInfo photoInfo = new PhotoInfo();
                            photoInfo.url = feedImgs;
                            photos.add(photoInfo);
                        }
                    }
//                String feedText = listBean.getFeedText();
                    if (photos!=null&&photos.size()>0){
                        holder.multiImageView.setVisibility(View.VISIBLE);
                        Glide.with(context).load(photos.get(0).url).centerCrop().into(holder.multiImageView);
                        // holder.multiImageView.setList(photos);
                        holder.tv_nums.setText(context.getResources().getString(R.string.gong)+photos.size()+context.getResources().getString(R.string.zhang));
                        holder.tv_content.setText(feedText);
                        holder.tv_content.setVisibility(View.VISIBLE);
                        holder.tvTxt.setVisibility(View.GONE);
                    }else {
                        photos.clear();
                        holder.tvTxt.setText(feedText);
                        holder.tvTxt.setVisibility(View.VISIBLE);
                        holder.tv_content.setVisibility(View.GONE);
                        holder.multiImageView.setVisibility(View.GONE);
                        holder.tv_nums.setText("");
                    }
       /* PhotoInfo photoInfo = new PhotoInfo();
        photoInfo.url = feedImgs ;
        List<PhotoInfo> photoInfos = new ArrayList<>();
        photoInfos.add(photoInfo);
        holder.multiImageView.setList(photoInfos);*/
                    if (photos.size()>0)
                        holder.multiImageView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                List<String> photoUrls = new ArrayList<String>();
                                for (PhotoInfo photoInfo : photos) {
                                    photoUrls.add(photoInfo.url);
                                }
                                //  ImagePagerActivity.ImageSize imageSize = new ImagePagerActivity.ImageSize(ScreenUtil.getScreenWidth(context),ScreenUtil.getScreenHeight(context));
                                //  ImagePagerActivity.startImagePagerActivity(((MycircleActivty) context), photoUrls, 0,imageSize);
                                Intent intent = new Intent(context, ImagePagerActivity.class);
                                intent.putExtra("circlebean",listBean);
                                context.startActivity(intent);
                            }
                        });
                    break;
                case 1 ://视频

                    if (!StringUtils.isEmpty(feedImgs)) {

                        PhotoInfo photoInfo = new PhotoInfo();
                        photoInfo.url = feedImgs;
                        photos.add(photoInfo);

                    }
                    holder.tv_nums.setVisibility(View.GONE);
                    if (photos!=null&&photos.size()>0){
                        holder.multiImageView.setVisibility(View.VISIBLE);
                        Glide.with(context).load(photos.get(0).url).centerCrop().into(holder.multiImageView);
                        // holder.multiImageView.setList(photos);
//                    holder.tv_nums.setText("共"+photos.size()+"张");
                        holder.tv_content.setText(feedText);
                        holder.tv_content.setVisibility(View.VISIBLE);
                        holder.tvTxt.setVisibility(View.GONE);
                    }else {
                        photos.clear();
                        holder.tvTxt.setText(feedText);
                        holder.tvTxt.setVisibility(View.VISIBLE);
                        holder.tv_content.setVisibility(View.GONE);
                        holder.multiImageView.setVisibility(View.GONE);
//                    holder.tv_nums.setText("");
                    }
                    holder.image_startvideo.setVisibility(View.VISIBLE);
                    if (photos.size()>0)
                        holder.multiImageView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(context, PlayNewActivity.class);
                                intent.putExtra("videoUrl", listBean.getFeedVideo());
                                context.startActivity(intent);
                            }
                        });
                    break;
                case 2 ://URL
                    break;
            }


            holder.layout_content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
               /* if (photos.size()>0){
                    List<String> photoUrls = new ArrayList<String>();
                    for (PhotoInfo photoInfo : photos) {
                        photoUrls.add(photoInfo.url);
                    }
                  //  ImagePagerActivity.ImageSize imageSize = new ImagePagerActivity.ImageSize(ScreenUtil.getScreenWidth(context),ScreenUtil.getScreenHeight(context));
                  //  ImagePagerActivity.startImagePagerActivity(((MycircleActivty) context), photoUrls, 0,imageSize);
                    Intent intent = new Intent(context, ImagePagerActivity.class);
                    intent.putExtra("circlebean",listBean);
                    context.startActivity(intent);
                }else {*/
                    Intent intent = new Intent(context, CirclealldetailActivity.class);
                    intent.putExtra("feedid",feedId);
                    context.startActivity(intent);
                    //  }
                }
            });
        }


    }
    private void setheadlayout(final HeaderViewHolder holder) {
        final List<MyCircleItem.ListBean> data = (List<MyCircleItem.ListBean>)datas;

        if (isShowGift)holder.image_gift.setVisibility(View.GONE);//对我的相册里面别人送我的礼物的按钮隐藏，如果以后需要可以改为显示，备注（SnsPopupWindow朋友圈送礼物的类SnsPopupWindow也要做修改才能送礼物）
        else holder.image_gift.setVisibility(View.GONE);

        holder.image_gift.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onbackgroudClickListenler.backgroudclick(v,2);
            }
        });

        if (!friendid.equals(ToolsUtils.getMyUserId())){
            holder.tvname.setText(friendname);
//            GlideUtils.loadCircleImage(context,friendurl,holder.imicon);
            Glide.with(context).load(friendurl).error(R.drawable.head_defaul).into(holder.imicon);
        }else {
            holder.imbg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onbackgroudClickListenler != null) {
                        onbackgroudClickListenler.backgroudclick(holder.imbg,0);
                    }
                }
            });
            holder.imicon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onbackgroudClickListenler.backgroudclick(holder.imbg,1);
                }
            });
            holder.tvname.setText(ToolsUtils.getUser().getName());
            Log.i("-----", "setheadlayout: ================"+ToolsUtils.getUser().getHeadUrl());
//            GlideUtils.loadCircleImage(context,ToolsUtils.getUser().getHeadUrl(),holder.imicon);
            Glide.with(context).load(ToolsUtils.getUser().getHeadUrl()).error(R.drawable.head_defaul).into(holder.imicon);
        }
        if (backgroudUrl!=null&&!StringUtils.isEmpty(backgroudUrl)){
            Glide.with(context).load(backgroudUrl).centerCrop().into(holder.imbg);
        }
    }

    @Override
    public int getItemCount() {

        return datas.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return TYPE_HEAD ;
        }else {
            return TYPE_BODY;
        }
    }
    public interface OnbackgroudClickListenler{
        void backgroudclick(View view,int type);//0 背景 1 头像  2 礼物   3 发布
    }
}
